<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Repair_model
 *
 * @author admin
 */
class repair_model extends CI_Model {

    private $_table_truck = 'vpc_truck';
    private $_table_driver = 'vpc_driver';
    private $_table_repair = 'vpc_repair';
    private $_table_cat_stock = 'vpc_catalog_autopart';
    private $_table_cat_status = 'vpc_catalog_status';
    private $_table_cat_prefix = 'vpc_catalog_prefix';
    private $_table_stock = 'vpc_stock';
    private $_table_repair_order = 'vpc_repair_order';

    public function list_truck_name() {
        $this->db->order_by('license_plate_code', 'asc');
        $query = $this->db->get_where($this->_table_truck);
        $arr_status = array();
        $arr_status[''] = 'เลือก';
        foreach ($query->result() as $list) {
            $arr_status[$list->id] = $list->license_plate_code;
        }
        return $arr_status;
    }

    public function save_data($truck_id, $driver_id = NULL, $repair_type, $detail, $created_date) {
        $arr_repair = array('truck_id' => $truck_id,
            'create_date' => $created_date,
            'driver_id' => $driver_id,
            'repair_detail' => $detail,
            'repair_status' => $repair_type, //รายการเบิก
            'created_by' => $this->session->userdata('username'),
            'created_date' => date("Y-m-d H:i:s")
        );

        $can_save = $this->db->insert($this->_table_repair, $arr_repair);
        return $can_save;
    }

    public function save_order_data($repair_id, $main_type_id, $sub_type_id, $brand_id, $stock_id, $qty, $total_price) {
        $arr_repair = array('repair_id' => $repair_id,
            'main_type_id' => $main_type_id,
            'sub_type_id' => $sub_type_id,
            'brand_id' => $brand_id,
            'stock_id' => $stock_id,
            'qty' => $qty,
            'total_price' => $total_price,
            'created_by' => $this->session->userdata('username'),
            'created_date' => date("Y-m-d H:i:s")
        );

        $can_save = $this->db->insert($this->_table_repair_order, $arr_repair);
        return $can_save;
    }

    public function get_last_data_repair() {
        $this->db->order_by('id', 'desc');
        $this->db->limit(1);
        $query = $this->db->get_where($this->_table_repair);
        return $query->result();
    }

    public function get_repair_by_id($id) {
        $this->db->select('*');
        $this->db->join($this->_table_cat_status . ' as status', 'status.id = repair.repair_status', 'left');
        $this->db->join($this->_table_driver . ' as driver', 'driver.id = repair.driver_id', 'left');
        $this->db->join($this->_table_truck . ' as truck', 'truck.id = repair.truck_id', 'left');
        $this->db->join($this->_table_cat_prefix . ' as prefix', 'prefix.id = driver.prefix_id', 'left');
        $query = $this->db->get_where($this->_table_repair . ' as repair', array('repair.id' => $id));
        return $query->result();
    }

    public function get_order_by_id($id) {
        $this->db->select('*');
        $this->db->select('a.name as main_type_name');
        $this->db->select('b.name as sub_type_name');
        $this->db->select('c.name as brand_name');
        $this->db->join($this->_table_cat_stock . ' as a', 'a.id = repair.main_type_id', 'left');
        $this->db->join($this->_table_cat_stock . ' as b', 'b.id = repair.sub_type_id', 'left');
        $this->db->join($this->_table_cat_stock . ' as c', 'c.id = repair.brand_id', 'left');
        $this->db->join($this->_table_stock . ' as stock', 'stock.id = repair.stock_id', 'left');
        $query = $this->db->get_where($this->_table_repair_order . ' as repair', array('repair_id' => $id));
        return $query->result();
    }

    public function get_repair_by_truck_id($id, $limit, $offset) {
        $this->db->select('*');
        $this->db->select('repair.id as repair_id');
        $this->db->select('status.status_name as status_name');
        $this->db->select('repair.create_date as made_date_repair');
        $this->db->join($this->_table_cat_status . ' as status', 'status.id = repair.repair_status', 'left');
        $this->db->join($this->_table_driver . ' as driver', 'driver.id = repair.driver_id', 'left');
        $this->db->join($this->_table_truck . ' as truck', 'truck.id = repair.truck_id', 'left');
        $this->db->join($this->_table_cat_prefix . ' as prefix', 'prefix.id = driver.prefix_id', 'left');
        $query = $this->db->get_where($this->_table_repair . ' as repair', array('repair.truck_id' => $id), $limit, $offset);
        return $query->result();
    }

    public function count_repair_by_truck_id($id) {
        $this->db->select('*');
        $query = $this->db->get_where($this->_table_repair . ' as repair', array('repair.truck_id' => $id));
        return count($query->result());
    }

    public function count($license_code = NULL, $order_type = NULL, $start_date = NULL, $end_date = NULL) {
        if (!is_null($license_code) && !empty($license_code)) {
            $this->db->like('truck.license_plate_code', $license_code);
        }
        if (!is_null($order_type) && !empty($order_type)) {
            $this->db->where('repair.repair_status', $order_type);
        }
        if (!is_null($start_date) && !empty($start_date)) {
            $this->db->where('DATE(repair.create_date) >=', $start_date);
        }
        if (!is_null($end_date) && !empty($end_date)) {
            $this->db->where('DATE(repair.create_date) <=', $end_date);
        }
        $this->db->join($this->_table_truck . ' as truck', 'truck.id = repair.truck_id', 'left');
        $query = $this->db->get_where($this->_table_repair . ' as repair');
        return count($query->result());
    }

    public function search_list_repair($license_code = NULL, $order_type = NULL, $start_date = NULL, $end_date = NULL, $limit = 10, $offset = 0) {
        $this->db->select('*');
        $this->db->select('repair.id as repair_id');
        $this->db->select('repair.create_date as repair_date');
        if (!is_null($license_code) && !empty($license_code)) {
            $this->db->like('truck.license_plate_code', $license_code);
        }
        if (!is_null($order_type) && !empty($order_type)) {
            $this->db->where('repair.repair_status', $order_type);
        }
        if (!is_null($start_date) && !empty($start_date)) {
            $this->db->where('DATE(repair.create_date) >=', $start_date);
        }
        if (!is_null($end_date) && !empty($end_date)) {
            $this->db->where('DATE(repair.create_date) <=', $end_date);
        }
        $this->db->join($this->_table_cat_status . ' as status', 'status.id = repair.repair_status', 'left');
        $this->db->join($this->_table_driver . ' as driver', 'driver.id = repair.driver_id', 'left');
        $this->db->join($this->_table_truck . ' as truck', 'truck.id = repair.truck_id', 'left');
        $query = $this->db->get_where($this->_table_repair . ' as repair', NULL, $limit, $offset);
        return $query->result();
    }

}
